//
//  HRTextFieldView.swift
//  HR
//
//  Created by zhenyu on 17/6/25.
//  Copyright © 2017年 allblue. All rights reserved.
//

import UIKit

enum FieldType {
  case normal
  case eyes
  case code
}

class HRTextFieldView: UIView {
  
  weak var textField: UITextField!
  weak var icon: UIImageView!
  weak var line: UIView!
  
  weak var eyes: UIImageView!
  weak var codeBtn: Button!
  
  var image = #imageLiteral(resourceName: "l_phone") {
    didSet {
      icon.image = image
    }
  }
  
  var placeHolder = "" {
    didSet {
      textField.placeholder = placeHolder
    }
  }
  
  var keyBoardType = UIKeyboardType.default {
    didSet {
      textField.keyboardType = keyBoardType
    }
  }

  // MARK: - LifeCycle
  convenience init(type: FieldType) {
    self.init(frame: CGRect.zero)
    setViews(type: type)
  }
  
  override init(frame: CGRect) {
    super.init(frame: frame)
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  // MARK: - SetViews
  func setViews(type: FieldType) {
    line = UIView().then({ (v) in
      addSubview(v)
      v.backgroundColor = Color.line
    }).then({ (v) in
      v.snp.makeConstraints({ (make) in
        make.bottom.equalTo(self.snp.bottom)
        make.left.right.equalTo(self)
        make.height.equalTo(0.5)
      })
    })
    
    icon = UIImageView(image: #imageLiteral(resourceName: "l_phone")).then({ (i) in
      addSubview(i)
    }).then({ (i) in
      i.snp.makeConstraints({ (make) in
        make.centerY.equalTo(self)
        make.left.equalTo(26)
      })
    })
    
    var rightOffset = -26
    switch type {
    case .eyes:
      rightOffset = -70
      
      eyes = UIImageView(image: #imageLiteral(resourceName: "l_eye_closed")).then({ (i) in
        addSubview(i)
      }).then({ (i) in
        i.snp.makeConstraints({ (make) in
          make.right.equalTo(self.snp.right).offset(-23)
          make.centerY.equalTo(self)
        })
      })
      
      break
    case .code:
      rightOffset = -102
      
      codeBtn = Button().then({ (b) in
        addSubview(b)
        b.title = "获取验证码"
        b.radius = 16
      }).then({ (b) in
        b.snp.makeConstraints({ (make) in
          make.right.equalTo(self)
          make.centerY.equalTo(self)
          make.width.equalTo(92)
          make.height.equalTo(32)
        })
      })
      break
    default:
      break
    }
    
    textField = UITextField().then({ (t) in
      addSubview(t)
      t.font = HRFont(size: 15)
      t.clearButtonMode = .whileEditing
    }).then({ (t) in
      t.snp.makeConstraints({ (make) in
        make.centerY.equalTo(self)
        make.bottom.equalTo(self)
        make.left.equalTo(icon.snp.left).offset(30)
        make.right.equalTo(self.snp.right).offset(rightOffset)
      })
    })
  }
}

extension HRTextFieldView {

  func eyesAction() {
  
  }
  
  func codeBtnAction() {
    
  }
}
